package com.cmnit.analysis.util

import java.util.Date

object ServiceUtil {

  /**
   * 将日期格式由yyyyMMdd或yyyyMMddHH转换成yyyy-MM-dd或yyyy-MM-dd HH:mm:ss
   *
   * @param acctTime 参数账期
   * @return
   */
  def transformationTimeFormat(acctTime: String): String = {
    if (acctTime.length == 8)
      DateUtil.getDate(DateUtil.getTimeByString(acctTime, DateUtil.FORMAT_MIN), DateUtil.FORMAT_SHORT)
    else if (acctTime.length == 10)
      DateUtil.getDate(DateUtil.getTimeByString(acctTime, DateUtil.FORMAT_MID), DateUtil.FORMAT_LONG)
    else
      "nothing"
  }

  /**
   * 获取上一小时账期时间
   *
   * @return
   */
  def getLastHourAcctTime(date: Date): String = {
    val year = DateUtil.getLastHourOfYear(date)
    val month = DateUtil.getLastHourOfMonth(date)
    val day = DateUtil.getLastHourOfDay(date)
    val hour = DateUtil.getLastHour(date)
    year + month + day + hour
  }

  /**
   * 获取前一天的账期时间
   *
   * @return
   */
  def getLastDayAcctDate(date: Date): String = {
    val year = DateUtil.getLastDayOfYear(date)
    val month = DateUtil.getLastDayOfMonth(date)
    val day = DateUtil.getLastDay(date)
    year + month + day
  }

  /**
   * 获取账期时间的SQL拼接
   *
   * @return
   */
  def getAcctInfo(acctTime: String): String = {
    // 拼接年份
    var sql = "year='" + acctTime.substring(0, 4) + "' "
    // 拼接月份
    if (acctTime.length >= 6) {
      sql += "and month='" + acctTime.substring(4, 6) + "' "
    }
    // 拼接日期
    if (acctTime.length >= 8) {
      sql += "and day='" + acctTime.substring(6, 8) + "' "
    }
    // 拼接小时
    if (acctTime.length >= 10) {
      sql += "and hour='" + acctTime.substring(8, 10) + "' "
    }
    sql
  }

  /**
   * 获取近4个账期的字符串拼接
   *
   * @param acctDate 账期时间
   * @return
   */
  def getAcctFourString(acctDate: String): String = {
    val date = DateUtil.getTimeByString(acctDate, DateUtil.FORMAT_MIN)
    var sql = "("
    for (i <- -3 to 0) {
      sql += "( " + getAcctInfo(DateUtil.getDateByAdding(date, i, DateUtil.FORMAT_MIN)) + ")"
      if (i != 0) sql += " OR "
    }
    sql += ")"
    sql
  }

  /**
   * 获取车牌号
   *
   * @param vehicleplate 车牌号+颜色
   * @return 返回所有设备数量
   */
  def getVlpUDF(vehicleplate: String): String = {
    vehicleplate.split("_")(0)
  }

  /**
   * 获取车牌颜色
   *
   * @param vehicleplate 车牌号+颜色
   * @return 返回所有设备数量
   */
  def getVlpcUDF(vehicleplate: String): String = {
    vehicleplate.split("_")(1)
  }
}
